
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>自定义ItemDecoration · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="ItemTouchHelper使用.html" />
    
    
    <link rel="prev" href="触摸事件.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../../">
            
                <a href="../../">
            
                    
                    前言
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../">
            
                <a href="../">
            
                    
                    Android
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" >
            
                <span>
            
                    
                    框架
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1.1" data-path="../框架/BlockCanary.html">
            
                <a href="../框架/BlockCanary.html">
            
                    
                    BlockCanary
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.1.2" data-path="../框架/fresco.html">
            
                <a href="../框架/fresco.html">
            
                    
                    Fresco
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.1.3" data-path="../框架/Glide源码.html">
            
                <a href="../框架/Glide源码.html">
            
                    
                    Glide
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.1.4" data-path="../框架/LeakCanary原理.html">
            
                <a href="../框架/LeakCanary原理.html">
            
                    
                    LeakCanary
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.1.5" data-path="../框架/Retrofit源码.html">
            
                <a href="../框架/Retrofit源码.html">
            
                    
                    Retrofit源码
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.2.2" >
            
                <span>
            
                    
                    源码解析
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.2.1" data-path="../源码/Binder.html">
            
                <a href="../源码/Binder.html">
            
                    
                    Binder
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2.2" data-path="../源码/Dialog源码.html">
            
                <a href="../源码/Dialog源码.html">
            
                    
                    Dialog源码
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2.3" data-path="../源码/RecycleView缓存机制.html">
            
                <a href="../源码/RecycleView缓存机制.html">
            
                    
                    RecycleView缓存机制
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2.4" data-path="../源码/WMS源码.html">
            
                <a href="../源码/WMS源码.html">
            
                    
                    WMS源码
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.2.3" >
            
                <span>
            
                    
                    自定义View及效果
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.3.1" data-path="触摸事件.html">
            
                <a href="触摸事件.html">
            
                    
                    触摸事件
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.2.3.2" data-path="自定义ItemDecoration.html">
            
                <a href="自定义ItemDecoration.html">
            
                    
                    自定义ItemDecoration
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3.3" data-path="ItemTouchHelper使用.html">
            
                <a href="ItemTouchHelper使用.html">
            
                    
                    ItemTouchHelper
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3.4" data-path="MaterilaDesign布局使用.html">
            
                <a href="MaterilaDesign布局使用.html">
            
                    
                    MaterilaDesign布局使用
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.2.4" >
            
                <span>
            
                    
                    Gradle
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.4.1" data-path="../Gradle/Gradle编译流程梳理.html">
            
                <a href="../Gradle/Gradle编译流程梳理.html">
            
                    
                    Gradle编译流程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4.2" data-path="../Gradle/Transform使用.html">
            
                <a href="../Gradle/Transform使用.html">
            
                    
                    Transform使用
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.2.5" >
            
                <span>
            
                    
                    kotlin实践
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.5.1" data-path="../Kotlin/协程使用.html">
            
                <a href="../Kotlin/协程使用.html">
            
                    
                    协程初步使用
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../../java/">
            
                <a href="../../java/">
            
                    
                    Java
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="../../java/多线程同步.html">
            
                <a href="../../java/多线程同步.html">
            
                    
                    多线程同步
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="../../java/垃圾回收机制.html">
            
                <a href="../../java/垃圾回收机制.html">
            
                    
                    垃圾回收机制
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="../../计算机网络/">
            
                <a href="../../计算机网络/">
            
                    
                    计算机网络
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="../../计算机网络/编码加密.html">
            
                <a href="../../计算机网络/编码加密.html">
            
                    
                    编码加密算法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="../../计算机网络/登陆认证.html">
            
                <a href="../../计算机网络/登陆认证.html">
            
                    
                    登陆认证流程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="../../计算机网络/http协议.html">
            
                <a href="../../计算机网络/http协议.html">
            
                    
                    http协议
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="../../操作系统/">
            
                <a href="../../操作系统/">
            
                    
                    操作系统
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6" >
            
                <span>
            
                    
                    设计模式
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.6.1" data-path="../../设计模式/单例模式.html">
            
                <a href="../../设计模式/单例模式.html">
            
                    
                    单例模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.2" data-path="../../设计模式/建造者模式.html">
            
                <a href="../../设计模式/建造者模式.html">
            
                    
                    建造者模式
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../.." >自定义ItemDecoration</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h2 id="recycleview">RecycleView</h2>
<h3 id="&#x81EA;&#x5B9A;&#x4E49;itemdecoration&#x5B9E;&#x73B0;&#x95F4;&#x8DDD;&#xFF1A;">&#x81EA;&#x5B9A;&#x4E49;ItemDecoration&#x5B9E;&#x73B0;&#x95F4;&#x8DDD;&#xFF1A;</h3>
<ul>
<li>1.&#x7EE7;&#x627F;RecyclerView.ItemDecoration&#x91CD;&#x5199;&#x5176;getItemOffsets&#x65B9;&#x6CD5;&#xFF1A;<pre><code>override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State)
</code></pre>&#x5176;&#x4E2D;&#x7B2C;&#x4E00;&#x4E2A;&#x53C2;&#x6570;outRect&#x4E3A;Decoration&#x95F4;&#x8DDD;&#x7684;&#x8DDD;&#x79BB;4&#x4E2A;&#x65B9;&#x5411;&#x7684;&#x8DDD;&#x79BB;&#xFF0C;&#x5728;RecycleView&#x5E03;&#x5C40;&#x65F6;&#x4F1A;&#x52A0;&#x8F7D;itemView&#x7684;padding&#x4E0A;&#xFF0C;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x4EE5;&#x4E0B;&#x4EE3;&#x7801;&#x8BBE;&#x7F6E;&#x8DDD;&#x79BB;<pre><code>outRect.set(left,top,right,bottom)
</code></pre></li>
<li><p>2.&#x9488;&#x5BF9;&#x4E0D;&#x540C;&#x5E03;&#x5C40;&#x7BA1;&#x7406;&#x5668;&#x517C;&#x5BB9;&#x95F4;&#x8DDD;
&#x5B8C;&#x6574;&#x4EE3;&#x7801;&#xFF1A;
```
class SpanItemDecoration(val left: Int = 0, val right: Int = 0, val top: Int = 0, val bottom: Int = 0) : RecyclerView.ItemDecoration() {</p>
<pre><code>  //&#x5B9E;&#x73B0;&#x8FB9;&#x8DDD;
  override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) {
      when (parent.layoutManager) {
          is GridLayoutManager -&gt; getGridLayoutItemOffsets(outRect, view, parent, state, (parent.layoutManager as GridLayoutManager).spanCount)
          is StaggeredGridLayoutManager -&gt; getStaggeredGridHorizontalItemOffsets(outRect, view, parent, state, (parent.layoutManager as StaggeredGridLayoutManager).spanCount, (parent.layoutManager as StaggeredGridLayoutManager).orientation)
          is LinearLayoutManager -&gt; getLinearLayoutItemOffsets(outRect, view, parent, state, (parent.layoutManager as LinearLayoutManager).orientation)
      }
  }

  /***
   * &#x9488;&#x5BF9;LinearLayoutManager&#x7684;&#x8FB9;&#x8DDD;
   */
  private fun getLinearLayoutItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State, orientation: Int) {
      val itemCount = parent.adapter?.itemCount ?: 0
      val currentCount = parent.getChildAdapterPosition(view)
      when (orientation) {
          LinearLayoutManager.HORIZONTAL -&gt; outRect.set(left.dp, top.dp, if (itemCount - 1 == currentCount) right.dp else 0, bottom.dp)
          LinearLayoutManager.VERTICAL -&gt; outRect.set(left.dp, top.dp, right.dp, if (itemCount - 1 == currentCount) bottom.dp else 0)
      }
  }

  /***
   * &#x9488;&#x5BF9;GridLayoutManager&#x548C;StaggeredGridLayoutManager&#x7684;VERTICAL&#x6A21;&#x5F0F;&#x7684;&#x8BBE;&#x7F6E;&#x8FB9;&#x8DDD;&#x79BB;
   */
  private fun getGridLayoutItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State, spanCount: Int) {
      val itemCount = parent.adapter?.itemCount ?: 0
      val currentCount = parent.getChildAdapterPosition(view)
      //&#x6BCF;&#x4E00;&#x884C;&#x7684;&#x6700;&#x540E;&#x4E00;&#x4E2A;View&#x624D;&#x9700;&#x8981;&#x6DFB;&#x52A0;right
      val lastOfLine = currentCount % spanCount == spanCount - 1 || currentCount == itemCount - 1
      outRect.set(left.dp, top.dp, if (lastOfLine) right.dp else 0, if (lastOfLine) bottom.dp else 0)
  }

  /***
   * &#x9488;&#x5BF9;StaggeredGridLayoutManager&#x7684;&#x8BBE;&#x7F6E;&#x8FB9;&#x8DDD;&#x79BB;
   */
  private fun getStaggeredGridHorizontalItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State, spanCount: Int, orientation: Int) {
      val params = view.layoutParams as StaggeredGridLayoutManager.LayoutParams
      val spanIndex = params.spanIndex StaggeredGridLayoutManager.LayoutParams
      val spanIndex = params.spanIndex
      val lastOfLine = spanIndex == spanCount - 1
      when (orientation) {
          RecyclerView.HORIZONTAL -&gt; {
              outRect.set(left.dp, top.dp, 0, if (lastOfLine) bottom.dp else 0)
          }
          RecyclerView.VERTICAL -&gt; {
              outRect.set(left.dp, top.dp, if (lastOfLine) right.dp else 0, 0)
          }
      }

  }
</code></pre></li>
</ul>
<pre><code>}
</code></pre><pre><code>&#x5176;&#x4E2D;&#x53EF;&#x4EE5;&#x91C7;&#x7528;**parent.adapter.itemCount**&#x83B7;&#x5F97;&#x6240;&#x6709;&#x5B50;View&#x7684;&#x6570;&#x91CF;&#xFF0C;&#x901A;&#x8FC7;**parent.getChildAdapterPosition**&#x83B7;&#x5F97;&#x8BE5;View&#x5728;adapter&#x4E2D;&#x7684;&#x4F4D;&#x7F6E;(&#x6240;&#x6709;item)&#xFF0C;&#x901A;&#x8FC7;**parent.getChildCount**&#x83B7;&#x5F97;&#x6240;&#x6709;&#x53EF;&#x89C1;&#x7684;item&#x7684;&#x6570;&#x91CF;
&#x9488;&#x5BF9;&#x7011;&#x5E03;&#x6D41;&#x5E03;&#x5C40;&#xFF0C;&#x9700;&#x8981;&#x4F7F;&#x7528;StaggeredGridLayoutManager.LayoutParams&#x4E2D;&#x7684;spanIndex&#x5C5E;&#x6027;&#x5224;&#x65AD;&#x8BBE;&#x7F6E;&#x7684;&#x7B2C;&#x51E0;&#x5217;&#x7684;&#x8FB9;&#x8DDD;&#x3002;


### &#x81EA;&#x5B9A;&#x4E49;ItemDecoration&#x5B9E;&#x73B0;&#x5206;&#x5272;&#x7EBF;&#x548C;&#x60AC;&#x6D6E;&#x5934;&#x90E8;&#x52A8;&#x753B;&#xFF1A;
#### &#x81EA;&#x5B9A;&#x4E49;ItemDecoration&#x5B9E;&#x73B0;&#x5206;&#x5272;&#x7EBF;&#xFF1A;
- 1. &#x5B9E;&#x73B0;RecyclerView.ItemDecoration&#x63A5;&#x53E3;&#xFF0C;&#x91CD;&#x5199;getItemOffsets&#x65B9;&#x6CD5;&#x4E3A;&#x5206;&#x5272;&#x7EBF;&#x7559;&#x51FA;&#x7A7A;&#x9699;&#x3002;
</code></pre><p>override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) {
            if (parent.layoutManager !is LinearLayoutManager) return
            val position = parent.getChildAdapterPosition(view)
            //&#x5982;&#x679C;&#x662F;&#x6700;&#x540E;&#x4E00;&#x4E2A;item&#x5C31;&#x4E0D;&#x9700;&#x8981;&#x753B;&#x5206;&#x5272;&#x7EBF;
            val needDivider = position != parent.adapter?.itemCount ?: 0 - 1
            outRect.bottom = if (needDivider) dividerHeight.dp else 0
        }</p>
<pre><code>- 2.&#x91CD;&#x5199;onDraw&#x65B9;&#x6CD5;&#x5BF9;&#x5F53;&#x524D;&#x5E03;&#x5C40;&#x5185;&#x7684;&#x6240;&#x6709;View&#x7ED8;&#x5236;&#x5206;&#x5272;&#x7EBF;
</code></pre><p> override fun onDraw(c: Canvas, parent: RecyclerView, state: RecyclerView.State) {
            val left: Int = parent.paddingLeft
            val right: Int = parent.width - parent.paddingRight
            //&#x83B7;&#x53D6;&#x5C4F;&#x5E55;&#x53EF;&#x89C1;&#x7684;view&#x6570;&#x91CF;
            val childCount: Int = parent.childCount
            for (i in 0 until childCount) {
                //&#x8BA1;&#x7B97;&#x5206;&#x5272;&#x7EBF;&#x8D77;&#x59CB;&#x4F4D;&#x7F6E;
                val child: View = parent.getChildAt(i)
                val params = child
                        .layoutParams as RecyclerView.LayoutParams
                // divider&#x7684;top &#x5E94;&#x8BE5;&#x662F; item&#x7684;bottom &#x52A0;&#x4E0A; marginBottom &#x518D;&#x52A0;&#x4E0A; Y&#x65B9;&#x5411;&#x4E0A;&#x7684;&#x4F4D;&#x79FB;  --&gt;&#x8BA1;&#x7B97;&#x8D77;&#x59CB;y&#x5750;&#x6807;
                val top = child.bottom + params.bottomMargin +
                        child.translationY.roundToInt()
                // divider&#x7684;bottom&#x5C31;&#x662F;top&#x52A0;&#x4E0A;divider&#x7684;&#x9AD8;&#x5EA6;&#x4E86;
                val bottom = (top + dividerHeight.dp)         //&#x8BA1;&#x7B97;&#x7ED3;&#x675F;y&#x5750;&#x6807;
                c.drawRect(left.toFloat(), top.toFloat(), right.toFloat(), bottom.toFloat(), paint) //&#x753B;&#x7B14;&#x81EA;&#x5B9A;&#x4E49;
            }
            super.onDraw(c, parent, state)</p>
<pre><code>    }
</code></pre><pre><code>
#### &#x5B9E;&#x73B0;&#x60AC;&#x6D6E;&#x5934;&#x90E8;&#xFF1A;
- 1. &#x5B9E;&#x73B0;&#x60AC;&#x6D6E;&#x539F;&#x7406;&#xFF1A;&#x7531;&#x4E8E;&#x60AC;&#x505C;&#x5934;&#x90E8;&#x9700;&#x8981;&#x5728;ItemView&#x4E4B;&#x4E0A;&#x663E;&#x793A;&#xFF0C;&#x6240;&#x4EE5;&#x7ED8;&#x5236;&#x8981;&#x91CD;&#x5199;onDrawOver&#x6765;&#x7ED8;&#x5236;&#x3002;&#x7B80;&#x5355;&#x7684;&#x56FE;&#x5F62;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;Canvas&#x7ED8;&#x5236;&#xFF0C;&#x590D;&#x6742;&#x7684;&#x5E03;&#x5C40;&#x9700;&#x8981;&#x4F7F;&#x7528;&#x81EA;&#x5B9A;&#x4E49;&#x7684;View&#x518D;&#x5C06;&#x5176;layout&#x540E;&#x518D;&#x4F7F;&#x7528;Canvas&#x7ED8;&#x5236;&#x51FA;&#x6765;&#x3002;&#x7531;&#x4E8E;&#x5982;&#x679C;&#x4F7F;&#x7528;onDrawOver&#xFF0C;&#x7ED8;&#x5236;&#x7684;view&#x4F1A;&#x8986;&#x76D6;&#x5728;itemView&#x4E4B;&#x4E0A;&#xFF0C;&#x8FD9;&#x4E2A;&#x65F6;&#x5019;&#x9700;&#x8981;&#x8DDF;&#x968F;RecycleView&#x6EDA;&#x52A8;&#x7684;&#x8BDD;&#xFF0C;&#x53EF;&#x4EE5;&#x91C7;&#x7528;Canvas&#x7684;translate&#x65B9;&#x6CD5;&#x5C06;&#x753B;&#x5E03;&#x79FB;&#x52A8;&#x518D;&#x7ED8;&#x5236;
</code></pre><pre><code>    //&#x4ECD;&#x7136;&#x9700;&#x8981;&#x5728;getItemOffsets&#x4E3A;&#x5176;&#x7559;&#x51FA;&#x7A7A;&#x95F4;&#xFF0C;&#x5426;&#x5219;&#x4F1A;&#x8986;&#x76D6;&#x5728;ItemView&#x4E4B;&#x4E0A;
    override fun onDrawOver(c: Canvas, parent: RecyclerView, state: RecyclerView.State) {
        super.onDrawOver(c, parent, state)
        //&#x83B7;&#x5F97;&#x5E03;&#x5C40;&#x4E2D;&#x7B2C;&#x4E00;&#x4E2A;View&#x7684;&#x4F4D;&#x7F6E;
        val child: View = parent.getChildAt(0)
        if (parent.getChildAdapterPosition(child) != 0) {
            //&#x83B7;&#x53D6;&#x662F;&#x5426;&#x662F;adapter&#x7B2C;&#x4E00;&#x4E2A;View --&gt;&#x8FD9;&#x91CC;&#x53EF;&#x4EE5;&#x63A7;&#x5236;&#x53EA;&#x5728;&#x7B2C;&#x4E00;&#x4E2A;Vie&#x65F6;&#x663E;&#x793A;&#x6216;&#x8005;&#x5206;&#x7EC4;&#x7684;&#x65F6;&#x5019;&#x663E;&#x793A;
            // &#x5982;&#x679C;&#x7B2C;&#x4E00;&#x4E2A;item&#x5DF2;&#x7ECF;&#x88AB;&#x56DE;&#x6536;,&#x6CA1;&#x6709;&#x663E;&#x793A;&#x5728;recycler view&#x4E0A;,&#x5219;&#x4E0D;&#x9700;&#x8981;draw header
            return
        }
        val params = child
                .layoutParams as RecyclerView.LayoutParams
        // divider&#x7684;top &#x5E94;&#x8BE5;&#x662F; item&#x7684;bottom &#x52A0;&#x4E0A; marginBottom &#x518D;&#x52A0;&#x4E0A; Y&#x65B9;&#x5411;&#x4E0A;&#x7684;&#x4F4D;&#x79FB;
        val top = child.top + params.topMargin -
                child.translationY.roundToInt()
        //&#x79FB;&#x52A8;&#x753B;&#x5E03;&#x8FBE;&#x5230;&#x7ED8;&#x5236;&#x7684;&#x6548;&#x679C;&#x968F;&#x5E03;&#x5C40;&#x79FB;&#x52A8;&#x7684;&#x6548;&#x679C; --&gt;&#x6BCF;&#x6B21;&#x6EDA;&#x52A8;&#x90FD;&#x4F1A;&#x8C03;&#x7528;&#x91CD;&#x7ED8;&#xFF0C;&#x4ECE;&#x800C;&#x6BCF;&#x6B21;&#x7684;&#x4F4D;&#x7F6E;&#x503C;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;itemView&#x7684;&#x9876;&#x90E8;&#x504F;&#x79FB;&#x7B97;&#x51FA;&#x753B;&#x5E03;&#x7684;&#x79FB;&#x52A8;
        c.translate(0.toFloat(), top - dividerHeight.dp.toFloat())
        //&#x6A21;&#x62DF;&#x4E00;&#x4E2A;&#x81EA;&#x5B9A;&#x4E49;&#x7684;View
        val view = TextView(parent.context).apply {
            text = &quot;1231412412412&quot;
            height = dividerHeight.dp
        }
        //&#x4E00;&#x5B9A;&#x8981;&#x5148;layout&#x81EA;&#x5B9A;&#x4E49;&#x7684;View
        view.layout(0, 0, 200.dp, dividerHeight.dp)
        view.draw(c)
        c.translate(0.toFloat(), -(top - dividerHeight.dp.toFloat()))
    }
</code></pre><pre><code>

#### &#x5B9E;&#x73B0;&#x7C98;&#x6027;&#x5934;&#x90E8;&#xFF1A;&#x5728;onDraw&#x4E2D;&#x7ED8;&#x5236;&#x6BCF;&#x4E2A;&#x5206;&#x7EC4;&#x7684;&#x5934;&#x90E8;&#xFF0C;&#x5728;onDrawOver&#x4E2D;&#x7ED8;&#x5236;&#x5E38;&#x9A7B;&#x7684;&#x7C98;&#x6027;&#x5934;&#x90E8;&#x5E76;&#x4E14;&#x6839;&#x636E;&#x5206;&#x7EC4;&#x7684;&#x6700;&#x540E;&#x4E00;&#x4E2A;&#x8DDD;&#x79BB;&#x9876;&#x4E0A;&#x7684;&#x8DDD;&#x79BB;&#x79FB;&#x52A8;&#x753B;&#x5E03;&#x5B9E;&#x73B0;&#x5728;&#x5206;&#x7EC4;&#x4EA4;&#x6362;&#x65F6;&#x5BF9;&#x5934;&#x90E8;&#x7684;&#x8FC7;&#x6E21;
- &#x4F7F;&#x7528;&#xFF1A;
</code></pre><p>rv.addItemDecoration(StickHeaderItemDecoration(R.layout.stick_view, 80).apply {
            setTransfer(object : StickHeadTransfer() {
                override fun isFirst(position: Int): Boolean {
                    //&#x5224;&#x5B9A;&#x4F4D;&#x7F6E;&#x662F;&#x5426;&#x4E3A;&#x5206;&#x7EC4;&#x7684;&#x7B2C;&#x4E00;&#x4E2A; &#x53EF;&#x4EE5;&#x6839;&#x636E;&#x81EA;&#x5DF1;&#x7684;&#x6570;&#x636E;&#x7C7B;&#x578B;&#x4F20;&#x9012;
                    return adapter.getData()[position].isFirst
                }</p>
<pre><code>            override fun isLast(position: Int): Boolean {
                //&#x5224;&#x5B9A;&#x4F4D;&#x7F6E;&#x662F;&#x5426;&#x4E3A;&#x5206;&#x7EC4;&#x7684;&#x6700;&#x540E;&#x4E00;&#x4E2A;
                return adapter.getData()[position].isLast
            }

            override fun bindData(stickView: View, position: Int) {
                //&#x7ED1;&#x5B9A;&#x89C6;&#x56FE;
                val data = adapter.getData()[position]
                stickView.findViewById&lt;TextView&gt;(R.id.tvTitle).text = data.description
            }

        })
    })    
</code></pre><pre><code>
- &#x81EA;&#x5B9A;&#x4E49;&#x7684;ItemDecoration&#xFF1A;
</code></pre><pre><code>abstract class StickHeadTransfer {
    abstract fun isFirst(position: Int): Boolean
    abstract fun isLast(position: Int): Boolean
    abstract fun bindData(stickView: View, position: Int)
}


class StickHeaderItemDecoration(private val layoutId: Int, private val stickViewHeight: Int = 0) : RecyclerView.ItemDecoration() {
    private var transfer: StickHeadTransfer? = null
    private lateinit var stickHeadView: View

    fun &lt;T : StickHeadTransfer&gt; setTransfer(transfer: T) {
        this.transfer = transfer
    }

    override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) {
        super.getItemOffsets(outRect, view, parent, state)
        //&#x83B7;&#x53D6;&#x7684;View&#x7684;&#x4F4D;&#x7F6E;
        initStickView(parent)
        if (transfer?.isFirst(parent.getChildAdapterPosition(view)) == true) {
            //&#x5934;&#x90E8;&#x6DFB;&#x52A0;&#x5BBD;&#x5EA6;
            outRect.top = stickViewHeight.dp
        }
    }

    override fun onDraw(c: Canvas, parent: RecyclerView, state: RecyclerView.State) {
        super.onDraw(c, parent, state)
        initStickView(parent)
        val left: Int = parent.paddingLeft
        val right: Int = parent.width - parent.paddingRight
        for (position in 0 until parent.childCount) {
            val child = parent.getChildAt(position)
            val adapterPosition = parent.getChildAdapterPosition(child)
            if (transfer?.isFirst(adapterPosition) == true) {
                //&#x8BA1;&#x7B97;ItemView&#x7684;&#x5B9E;&#x9645;top
                val bottom = getViewRealTop(child)
                val top = bottom - stickViewHeight.dp
                val view = LayoutInflater.from(parent.context).inflate(layoutId, parent, false)
                //&#x5148;&#x7ED1;&#x5B9A;&#x6570;&#x636E;&#x518D;&#x6D4B;&#x91CF;
                transfer?.bindData(view, adapterPosition)
                view.measure(child.measuredWidth, child.measuredHeight)
                view.layout(left, 0, right, stickViewHeight.dp)
                c.translate(0.toFloat(), top.toFloat())
                view.draw(c)
                c.translate(0.toFloat(), -top.toFloat())
            }
        }
    }

    override fun onDrawOver(c: Canvas, parent: RecyclerView, state: RecyclerView.State) {
        super.onDrawOver(c, parent, state)
        if (parent.childCount &lt;= 0) return
        initStickView(parent)
        /***
         * 1&#x3001;&#x4E0D;&#x662F;&#x7B2C;&#x4E00;&#x4E2A;&#x9700;&#x8981;&#x6DFB;&#x52A0;&#x4E00;&#x4E2A;&#x60AC;&#x6D6E;&#x7684;View&#x3002;
         * 2&#x3001;&#x662F;&#x6700;&#x540E;&#x4E00;&#x4E2A;&#x9700;&#x8981;&#x6DFB;&#x52A0;&#x4E00;&#x4E2A;&#x968F;&#x7740;&#x5176;&#x7684;Top&#x9010;&#x6E10;&#x53D8;&#x5316;&#x7684;View
         */
        val left: Int = parent.paddingLeft
        val right: Int = parent.width - parent.paddingRight
        val child = parent.getChildAt(0)
        val adapterPosition = parent.getChildAdapterPosition(child)
        //&#x5148;&#x7ED1;&#x5B9A;&#x6570;&#x636E;&#x518D;&#x6D4B;&#x91CF;
        transfer?.bindData(stickHeadView, adapterPosition)
        stickHeadView.measure(child.measuredWidth, child.measuredHeight)
        stickHeadView.layout(left, 0, right, stickViewHeight.dp)
        if (!beginTransfer(c, parent)) stickHeadView.draw(c)

    }


    private fun beginTransfer(c: Canvas, parent: RecyclerView): Boolean {
        //&#x5224;&#x65AD;&#x5206;&#x7EC4;&#x7684;&#x6700;&#x540E;&#x4E00;&#x4E2A;&#x8DDD;&#x79BB;&#x5934;&#x90E8;&#x7684;&#x8DDD;&#x79BB;&#x5224;&#x65AD;&#x662F;&#x5426;&#x5F00;&#x59CB;&#x79FB;&#x52A8;
        for (position in 0 until parent.childCount) {
            val view = parent.getChildAt(position)
            if (transfer?.isLast(parent.getChildAdapterPosition(view)) == true) {
                val top = getViewRealTop(view)
                val distance = top + view.height
                if (distance &gt; stickViewHeight.dp) return false
                //&#x7531;&#x4E8E;&#x60AC;&#x505C;&#x5E03;&#x5C40;&#x9700;&#x8981;&#x8986;&#x76D6;&#x5230;&#x6700;&#x540E;&#x4E00;&#x4E2A;Item&#x5168;&#x90E8;&#x6240;&#x4EE5;&#x591A;&#x79FB;&#x52A8;&#x4E00;&#x4E2A;&#x7684;height&#x9AD8;&#x5EA6;
                c.translate(0.toFloat(), (top - view.height).toFloat())
                stickHeadView.draw(c)
                c.translate(0.toFloat(), -(top - view.height).toFloat())
                return true
            }
        }
        return false
    }

    private fun getViewRealTop(child: View): Int {
        val params = child
                .layoutParams as RecyclerView.LayoutParams
        return child.top + params.topMargin +
                child.translationY.roundToInt()
    }

    private fun initStickView(parent: RecyclerView) {
        if (this::stickHeadView.isInitialized) return
        else stickHeadView = LayoutInflater.from(parent.context).inflate(layoutId, parent, false)
    }
}
</code></pre><p>```</p>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="触摸事件.html" class="navigation navigation-prev " aria-label="Previous page: 触摸事件">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="ItemTouchHelper使用.html" class="navigation navigation-next " aria-label="Next page: ItemTouchHelper">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"layout":"post","title":"自定义ItemDecoration","subtitle":"自定义ItemDecoration","date":"2020-06-28T19:10:00.000Z","author":"hzy","//header-img":"img/post-bg-2015.jpg","catalog":true,"tags":["Android","布局","RecycleView"],"level":"1.2.3.2","depth":3,"next":{"title":"ItemTouchHelper","level":"1.2.3.3","depth":3,"path":"android/自定义View及效果/ItemTouchHelper使用.md","ref":"android/自定义View及效果/ItemTouchHelper使用.md","articles":[]},"previous":{"title":"触摸事件","level":"1.2.3.1","depth":3,"path":"android/自定义View及效果/触摸事件.md","ref":"android/自定义View及效果/触摸事件.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":["livereload"],"pluginsConfig":{"livereload":{},"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"android/自定义View及效果/自定义ItemDecoration.md","mtime":"2020-08-16T16:13:19.000Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2021-03-13T16:08:39.790Z"},"basePath":"../..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../../gitbook/gitbook.js"></script>
    <script src="../../gitbook/theme.js"></script>
    
        
        <script src="../../gitbook/gitbook-plugin-livereload/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

